//
// Created by xili on 2024/8/16 19:11.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    int longestAlternatingSubarray(vector<int> &nums, int threshold) {
        int l = 0, r = 0;
        int n = nums.size();
        int ans = 0;
        for (; l < n; l++) {
            if (nums[l] & 1 || nums[l] > threshold) {
                r++;
                continue;
            }
            while (r + 1 < n && nums[r] % 2 != nums[r + 1] % 2 && nums[r + 1] <= threshold) {
                r++;
            }
            ans = max(ans, r - l + 1);
            l = r++;
        }
        return ans;
    }
};